package com.mengma.ec.controller.portal;

import com.mengma.ec.commons.Const;
import com.mengma.ec.commons.ServerResponse;
import com.mengma.ec.entity.User;
import com.mengma.ec.service.portal.IUserService;
import com.mengma.ec.util.MD5Util;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.sql.rowset.serial.SerialBlob;

/**
 * @Auther: Tangzhiqiang
 * @Date: 2018/9/12 0012 14:23
 * @Description: 门户，用户controller
 */
@RequestMapping("/user/")
@RestController
public class UserController {

    @Autowired
    private IUserService iUserService;

    /**
     * 门户用户登录
     *
     * @param:
     * @return:
     * @auther: Tangzhiqiang
     * @date: 2018/9/12 下午 14:28
     */
    @PostMapping(value = "login.do")
    public ServerResponse login(@RequestBody User user, HttpSession session) {
        ServerResponse<User> response = iUserService.login(user);
        // 判断登录是否成功
        if (response.isSuccess()) {
            session.setAttribute(Const.CURRENT_USER, response.getData());
        }
        return response;
    }

    @PostMapping(value = "register.do")
    public ServerResponse register(@RequestBody User user){
        ServerResponse<User> response = iUserService.register(user);
        return response;
    }

    @PostMapping(value = "logout.do")
    public ServerResponse logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.createBySuccessMessage("登出成功！");
    }

    /**
     *检查用户名是否有效
     * 前台页面传入的是表单字段，不是json数据格式
     */
    @PostMapping("check_valid.do")
    public ServerResponse check_valid(@RequestParam(name="string",required = true)String str,@RequestParam(name="type",required = true)String type){
        ServerResponse response = iUserService.check_valid(str,type);
        return response;
    }


    /**
     * 获取当前登录用户信息
     * @param session
     * @return
     */
    @PostMapping("get_user_info.do")
    public  ServerResponse<User> get_user_info(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);

        if (user == null){
            return ServerResponse.createByErrorMessage("脑子是个好东西！请先登录！");
        }
        return ServerResponse.createBySuccess("获取成功",user);
    }

    @PostMapping("forget_get_question.do")
    public ServerResponse<String> forget_get_question(String username){
        String quetion = iUserService.forget_get_question(username);
        return ServerResponse.createBySuccess(quetion);
    }

    @GetMapping("forget_get_answer.do")
    public ServerResponse<String> forget_get_answer(@RequestParam(required = true)String username,
                                                    @RequestParam(required = true) String question,
                                                    @RequestParam(required = true)String answer){
        ServerResponse<String> response = iUserService.forget_get_answer(username,question,answer);
        return response;
    }

//    忘记密码的重设密码
    @PostMapping("forget_reset_password.do")
    public ServerResponse<String> forget_get_password(@RequestParam(required = true)String username,
                                                    @RequestParam(required = true) String passwordNew,
                                                    @RequestParam(required = true)String forgetToken){
        ServerResponse<String> response = iUserService.forget_get_password(username,passwordNew,forgetToken);
        return response;
    }

    @PostMapping("reset_password.do")
    public ServerResponse<String> reset_password(@RequestParam(required = true)String passwordOld,
                                                 @RequestParam(required = true)String passwordNew,
                                                 HttpSession session){
        ServerResponse<String> response = iUserService.reset_password(passwordOld,passwordNew,session);
        return response;
    }

//    登录状态更新个人信息
    @PostMapping("update_information.do")
    public ServerResponse<User> updata_information(@RequestBody User user,HttpSession session){
        ServerResponse<User> response = iUserService.updata_information(user,session);
        return response;
    }

//    获取当前登录用户的详细信息，并强制登录
    @PostMapping("get_information.do")
    public ServerResponse<User> get_information(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorMessage("脑子是个好东西！请先登录！");
        }
        ServerResponse<User> response = iUserService.get_information(user);
        return response;
    }
}
